
/**
 * 写cookies
 * @param {[String]}
 * @param {[String]}
 */
function setCookie(name, value, days) {
	var Days = days || 30; //此 cookie 将被保存 30 天
	var exp = new Date(); //new Date("December 31, 9998");
	exp.setTime(exp.getTime() + 60 * 60 * 1000 *24*30); 
	document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + "; path=/";
}; 
/**
 * 清除指定cookie 
 * @param  {[String]}
 */
function clearCookie(name) {  
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString()+"; path=/";
} ; 
/**
 * 获取cookie
 * @param  {[String]}
 */
function getCookie(name) {  
	var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
	if (arr != null) return unescape(arr[2]);
	return null;
}  
/**
 * 清除所有cookie
 */
function clearAllCookie(){ 
	var cookies = document.cookie.split(";");
	for (var i = 0; i < cookies.length; i++) {
		var cookie = cookies[i];
		var eqPos = cookie.indexOf("=");
		var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
		document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
	}
	if (cookies.length > 0) {
		for (var i = 0; i < cookies.length; i++) {
			var cookie = cookies[i];
			var eqPos = cookie.indexOf("=");
			var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
			var domain = location.host.substr(location.host.indexOf('.'));
			document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=" + domain;
		}
	}
} 


/**
 * 检测是否是微信浏览器
 * @return {Boolean}
 */
function isWeiXin () {
	var UA = navigator.userAgent;
	if ( UA.match(/MicroMessenger/i) == 'MicroMessenger' ) {
		return true;
	}
	return false;
}
/**
 * 微信授权地址跳转
 */
function wXAuthJump() {
	var urls=window.location.href;
	if(GetQueryString('code')){
		var urlss=urls.split('?');
		urls=urlss[0];
	}
	var sendData = {
		RETURN_URL:urls
	}
	$.ajax({
        url : getIp + '/CARNT/UserWeb/authorization.action',
        type : "post",
        dataType : "json",
        data :sendData,
        success : function(data) {	 
        	clearCookie('_unionidpo_');
			location.href=data.RETURN_URL;
        }
    });
}

/**
 * 获取URL参数
 * @param {[String]}
 */
function GetQueryString(name){
	var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if(r!=null)return  unescape(r[2]); return null;
}
/**
 * 发送openid或code
 */
function sendWXCodeOrOpenid() {
	var searchCode = GetQueryString('code');
	var userid=getCookie('loginkey');
	var mobile=getCookie('userName');
	var unionid=getCookie('_unionidpo_');
	if(unionid){
		searchCode='';
	}else{
		unionid='';
	}
	var sendData = {
		CODE: searchCode,
		MOBILE: mobile===undefined?'':mobile,
		USER_ID:userid===undefined?'':userid,
		UNIONID:unionid
	}
	$.ajax({
        url : getIp + '/CARNT/UserWeb/authorization.action',
        type : "post",
        dataType : "json",
        data :sendData,
        success : function(data) {	 
          if ( data.STATUS_CODE == 1) {
	            //获取用户微信信息
	        setCookie('_unionidpo_', data.AUTH_INFO.unionid);//用户的unionid
	        setCookie('_ofninepo_', data.AUTH_INFO.openid);
	        setCookie("userN",data.AUTH_INFO.nickname);
	        if(data.USER_ID){	
	        	setCookie("userid",data.USER_ID);
		        setCookie("userName",data.MOBILE);
		        setCookie("loginkey",data.USER_ID);
	        }
          }else if(data.STATUS_CODE == 2){
            clearCookie('_unionidpo_');
            wXAuthJump();
          }
        }
    });

}
var unionids = getCookie('_unionidpo_');
var codes = GetQueryString('code');
if(_Global_=='web'&&isWeiXin()){
	if(unionids){
		sendWXCodeOrOpenid();
	}else{
		if(codes){
			sendWXCodeOrOpenid();
		}else{
			wXAuthJump();
		}
	}
}

